package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import services.CentralVendorCRUDService;
import services.ProtocolCRUDService;
import services.ProtocoloCompromisopaisService;
import services.VendorTypeService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay
 * Date: 11/6/12
 * Time: 10:25 AM
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Protocols/GlobalVendorSelection")
public class GlobalVendorSelectionController extends ActionSupport implements ModelDriven {

    private Vendorcentral vendor;
    private CentralVendorCRUDService centralVendorService;
    private ProtocolCRUDService protocolCRUDService;
    private List<Vendorcentral> list;
    private List<Compromisopais> countries;
    private ProtocolCRUDService protocolService;
    private Protocolo protocoloRecovered;
    private ProtocoloCompromisopaisService protocoloCompromisopaisService;
    private List<VendorType> vendorTypes;
    private VendorTypeService vendorTypeService;
    private String errorMessage;
    private List<Integer> chosenCountries;

    @Autowired
    public GlobalVendorSelectionController(CentralVendorCRUDService centralVendorService, ProtocolCRUDService protocolCRUDService, ProtocolCRUDService protocolService, ProtocoloCompromisopaisService protocoloCompromisopaisService, VendorTypeService vendorTypeService) {
        this.centralVendorService = centralVendorService;
        this.protocolCRUDService = protocolCRUDService;
        this.protocolService = protocolService;
        this.protocoloCompromisopaisService = protocoloCompromisopaisService;
        this.vendorTypeService = vendorTypeService;
        vendor = new Vendorcentral();
        list = new ArrayList<Vendorcentral>();
        vendorTypes =  this.vendorTypeService.findAll();
    }

    private void cargarProtocoloYPaises(){
        try{
            //protocoloCompr = protocolService.recoverProtocoloCompromisopais();
            protocoloRecovered = protocolService.recoverProtocolo();
        }
        catch (NoProtocoloCompromisopaisSelectedException e){
            e.printStackTrace();
        }
        if(protocoloRecovered != null){
            this.countries = protocoloCompromisopaisService.findCompromisoPaisByProtocoloId(protocoloRecovered.getId());
        }
    }


    @Action(value="main", results={
            @Result(name="success", location="/protocols.globalVendorSelection.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        cargarProtocoloYPaises();
        list = centralVendorService.findAllCentralVendorsByProtocolo(protocoloRecovered);
        if(vendor.getId()!= null){
            vendor = centralVendorService.findById(vendor.getId());
            chosenCountries = new ArrayList<Integer>(0);
            for(ProtocoloCompromisopais pc : vendor.getProtocolos()){
                chosenCountries.add(pc.getCompromisopais().getId());
            }
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction"),
            @Result(name="input", location="/protocols.globalVendorSelection.tiles", type="tiles")
    })
    public String save() throws NoProtocoloCompromisopaisSelectedException {
        try {
          cargarProtocoloYPaises();
            List<ProtocoloCompromisopais> pcps =
                    protocoloCompromisopaisService.finByIds(protocoloRecovered,chosenCountries);
            vendor.setProtocolos(pcps);
            centralVendorService.saveGlobalVendor(vendor,protocoloRecovered);

        }catch (ConcurrencyFailureException cfe){
            errorMessage = "Someone has just saved the same record you did";
            execute();
            return INPUT;
        }
        return SUCCESS;
    }

    public Vendorcentral getVendor() {
        return vendor;
    }

    public void setVendor(Vendorcentral vendor) {
        this.vendor = vendor;
    }

    public List<Vendorcentral> getList() {
        return list;
    }

    public void setList(List<Vendorcentral> list) {
        this.list = list;
    }

    @Override
    public Vendorcentral getModel() {
        return vendor;
    }

    public Protocolo getProtocoloRecovered() {
        return protocoloRecovered;
    }

    public void setProtocoloRecovered(Protocolo protocoloRecovered) {
        this.protocoloRecovered = protocoloRecovered;
    }

    public List<Compromisopais> getCountries() {
        return countries;
    }

    public void setCountries(List<Compromisopais> countries) {
        this.countries = countries;
    }

    public List<VendorType> getVendorTypes() {
        return vendorTypes;
    }

    public void setVendorTypes(List<VendorType> vendorTypes) {
        this.vendorTypes = vendorTypes;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public List<Integer> getChosenCountries() {
        return chosenCountries;
    }

    public void setChosenCountries(List<Integer> chosenCountries) {
        this.chosenCountries = chosenCountries;
    }
}
